<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function merge(intervals) {
            // write code here
            let res = []
            intervals.sort((a, b) => a[0] - b[0])
            let pre = intervals[0]
            for (let i = 1; i < intervals.length; i++) {
                let cur = intervals[i]
                if (cur[0] <= pre[1]) {
                    pre[1] = Math.max(pre[1], cur[1])
                } else {
                    res.push(pre)
                    pre = cur
                }

            }
            res.push(pre)
            return res
        }
        console.log(merge([[1, 2], [0, 4]]));
    </script>
</body>

</html>